package fun.tan90.easy.binlog.plugin.common.listener;

import cn.hutool.core.util.ReUtil;
import fun.tan90.easy.binlog.base.enums.OutputResEnum;
import fun.tan90.easy.binlog.plugin.common.event.BinlogDataEvent;
import fun.tan90.easy.binlog.plugin.common.event.BinlogDataEventRes;
import fun.tan90.easy.binlog.plugin.common.model.BinlogData;
import fun.tan90.easy.binlog.plugin.common.model.BinlogOutputRule;
import org.noear.dami.bus.Payload;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/9/4 15:10
 */
public interface BinlogEventPluginListener {

    String outputType();

    default boolean filterOutput(BinlogOutputRule binlogOutputRule, BinlogData binlogData) {
        String database = binlogData.getDatabase();
        String table = binlogData.getTable();
        String schemaRegular = binlogOutputRule.getSchemaRegular();
        String tableRegular = binlogOutputRule.getTableRegular();
        return ReUtil.isMatch(schemaRegular, database) && ReUtil.isMatch(tableRegular, table);
    }

    default void reply(Payload<BinlogDataEvent, BinlogDataEventRes> payload, String outputRuleCountId, OutputResEnum outputRes) {
        if (!payload.isSubscribe()) {
            return;
        }
        payload.reply(BinlogDataEventRes.builder()
                .outputCountId(outputRuleCountId)
                .outputRes(outputRes)
                .outputType(outputType())
                .build());
    }
}
